﻿using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace Utilities.Configuration
{
    public abstract class ConfigurationElementCollection<TConfigurationElement, TConfigurationElementKey> : ConfigurationElementCollection, IEnumerable<TConfigurationElement>
        where TConfigurationElement : ConfigurationElement, new()
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new TConfigurationElement();
        }

        protected abstract TConfigurationElementKey GetElementKey(TConfigurationElement element);

        protected override object GetElementKey(ConfigurationElement element)
        {
            return GetElementKey((TConfigurationElement)element);
        }

        public void Add(TConfigurationElement configurationElement)
        {
            BaseAdd(configurationElement);
        }

        public void Remove(TConfigurationElementKey configurationElementKey)
        {
            BaseRemove(configurationElementKey);
        }

        public TConfigurationElement this[TConfigurationElementKey configurationElementKey]
        {
            get { return (TConfigurationElement)BaseGet(configurationElementKey); }
        }

        public TConfigurationElement this[int index]
        {
            get { return (TConfigurationElement)BaseGet(index); }
        }

        public new IEnumerator<TConfigurationElement> GetEnumerator()
        {
            IEnumerator enumerator = (((IEnumerable)this).GetEnumerator());
            while (enumerator.MoveNext())
            {
                yield return (TConfigurationElement)enumerator.Current;
            }
        }
    }
}
